package com.sofwin.controller;

import com.sofwin.Constants;
import com.sofwin.system.dto.DeptDto;
import com.sofwin.system.dto.ResourceDto;
import com.sofwin.system.pojo.SysDept;
import com.sofwin.system.pojo.SysRole;
import com.sofwin.system.pojo.SysUser;
import com.sofwin.system.service.DeptService;
import com.sofwin.system.service.RoleService;
import com.sofwin.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2021/7/13 10:43
 * @email 115176513@qq.com
 * @description: 用于转发到对应的视图
 */
@Controller
public class ViewController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("view/user/data")
    public String forward(String viewPath) {
        return "user/data";
    }

    @RequestMapping("view/user/edit")
    public String forward2(SysUser user, ModelMap mmap) {
        if (user != null && user.getSysId() != null) {
            user = userService.queryUserById(user.getSysId());
            mmap.put("user", user);
        }
       List<SysRole> roles =roleService.queryAllRoles();
        List<SysDept> sysDepts = deptService.queryDepts(null);
        mmap.put("depts",sysDepts);
        mmap.put("roles",roles);
        return "user/edit";
    }

    @RequestMapping("index")
    public String index(HttpSession session,ModelMap  mmap) {
        SysUser user = (SysUser)session.getAttribute(Constants.CURR_USER);
        // 角色Id
        List<ResourceDto> resources = roleService.queryResource(user.getRoleId());
        // 获取到用户悉尼
        mmap.put("resources",resources);
        return "index";
    }

    /**
     * 跳转到角色管理
     */
    @RequestMapping("view/role/data")
    public String roleData() {
        return "role/data";
    }

    @RequestMapping("view/role/edit")
    public String roleEdit(Integer id, ModelMap mmap) {
        mmap.put("role", roleService.queryRoleById(id));
        return "role/edit";
    }

    @RequestMapping("view/dept/data")
    public String deptData() {
        return "dept/data";
    }

    @RequestMapping("view/dept/edit")
    public String deptEdit(Integer id, ModelMap mmap) {
        SysDept dept = new SysDept();
        dept.setStatus(1);
        List<SysDept> depts = deptService.queryDepts(dept);
        mmap.put("depts", depts);
        DeptDto dept2 = deptService.queryById(id);
        mmap.put("dept", dept2);
        return "dept/edit";
    }

    @RequestMapping("view/dept/tree")
    public String deptTree(ModelMap mmap) {

        return "dept/tree";
    }

    @RequestMapping("view/role/permssion")
    public String rolePermission(Integer roleId,ModelMap mmap){
        mmap.put("roleId",roleId);
        // 查到资源的集合  再使用jackon转为json字符串
        // 放到作用域  mmap.put("str",....);
        return "role/permission";
    }

}
